package com.gwcloud.leetcode.tags.dp;

/** 
* @ClassName   : Leetcode0123 
* @author      : ganwei 
* @date        : 2020年5月17日 
* @Description : 123. 买卖股票的最佳时机 III 
*/
public class Leetcode0123 {
    public int maxProfit(int[] prices) {
    	int d_i10 = 0 , d_i11 = Integer.MIN_VALUE;
    	int d_i20 = 0 , d_i21 = Integer.MIN_VALUE;
    	for(int i = 0; i < prices.length; i++) {
    		d_i20 = Math.max(d_i20, d_i21 + prices[i]);
    		d_i21 = Math.max(d_i21, d_i10 - prices[i]);
    		d_i10 = Math.max(d_i10, d_i11 + prices[i]);
    		d_i11 = Math.max(d_i11, -prices[i]);
    	}
    	
    	return d_i20;
    }
    
    public static void main(String[] args) {
    	Leetcode0123 l0123 = new Leetcode0123();
    	int[] prices = {3,3,5,0,0,3,1,4};
    	System.out.println(l0123.maxProfit(prices));
    }
}
